package com.controller;

import com.entity.User;
import com.service.UserService;
import com.utils.MD5Util;
import com.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录业务
     *
     * @param user
     * @param session
     * @return
     */
    @RequestMapping(value = "/login.do", method = RequestMethod.POST) // 表单提交过来的路径
    public String userLogin(User user, HttpSession session) {
        user.setPassword(MD5Util.MD5(user.getPassword()));
        User result = userService.userLogin(user);
        if (result != null) {
            session.setAttribute("user", result);
            return "/user/userIndex";
        } else {
            return "/user/userLogin";
        }
    }

    /**
     * 用户注销登录
     *
     * @param session
     * @param sessionStatus
     * @return
     */
    @RequestMapping("/userOutLogin.do")
    public String userOutLogin(HttpSession session, SessionStatus sessionStatus) {
        session.removeAttribute("user");
        session.invalidate();
        sessionStatus.setComplete();
        return "/user/userLogin";
    }

    /**
     * 更新用户状态
     * @param session
     * @return
     */
    @RequestMapping("/userInfo.do")
    public String userInfo(HttpSession session){
        User user = (User) session.getAttribute("user");
        session.setAttribute("user",userService.userFindUser(user.getId()));
        return "/user/userInfo";
    }
    /**
     * 提交修改用户基础信息
     *
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("/userEdit.do")
    public ModelAndView userEdit(User user, HttpSession session) {
        userService.userEdit(user);
        User result = userService.userFindUser(user.getId());
        session.setAttribute("user", result);
        return new ModelAndView("redirect:/userInfo.do");
    }

    /**
     * 用户修改密码
     *
     * @param user
     * @return
     */
    @RequestMapping("/userPwd.do")
    public ModelAndView userPwd(User user) {
        userService.userPwd(user.getId(), MD5Util.MD5(user.getPassword()));
        return new ModelAndView("redirect:/userInfo.do");
    }

    /**
     * 用户提交解封申请
     * @param user
     * @return
     */
    @RequestMapping("/userApplyStatus.do")
    public ModelAndView userApplyStatus(User user){
        userService.userApplyStatus(user);
        return new ModelAndView("redirect:/userInfo.do");
    }
    @RequestMapping("/uploadImage")
    public ModelAndView uploadImage(MultipartFile file,User user, HttpServletRequest request) throws IOException {

        //String name = UUID.randomUUID().toString().replaceAll("-", "");
        //设置图片上传路径,是目标文件夹的路径
        String filePath = request.getSession().getServletContext().getRealPath("./static/images/");
        System.out.println("filePath:::"+filePath);
        // 获取原始图片的扩展名
        String originalFilename = file.getOriginalFilename();
        System.out.println("originalFilename:::"+originalFilename);

        // 生成文件新的名字
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + originalFilename;
        System.out.println("newFileName:::"+newFileName);

        // 封装上传文件位置的全路径
        File targetFile = new File(filePath, newFileName);
        file.transferTo(targetFile);

        // 保存到数据库
        user.setPic(newFileName);
        userService.uploadImage(user);
        //重定向到查看图片的方法上去
        return new ModelAndView("redirect:/userInfo.do");
    }

}


